//
//  UITabBar+Badge.m
//  ICome
//
//  Created by ENN on 16/5/10.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "UITabBar+Badge.h"

#define TabbarItemNums [[[NSUserDefaults standardUserDefaults] valueForKey:@"ICTabbarItemNums"] intValue]

@implementation UITabBar (Badge)

- (void)showBadgeOnItemIndex:(int)index
{
    [self removeBadgeOnItemIndex:index];
    
    UIView *badgeView = [[UIView alloc]init];
    badgeView.tag = 888 + index;
    badgeView.layer.cornerRadius = 5;
    badgeView.backgroundColor = [UIColor redColor];
    CGRect tabFrame = self.frame;
    
    float percentX = (index +0.57) / TabbarItemNums;
    CGFloat x = ceilf(percentX * tabFrame.size.width);
    CGFloat y = ceilf(0.12 * tabFrame.size.height);
    badgeView.frame = CGRectMake(x, y, 10, 10);
    [self addSubview:badgeView];

}

- (void)hideBadgeOnItemIndex:(int)index
{
    //移除小红点
    [self removeBadgeOnItemIndex:index];
    
}



- (void)removeBadgeOnItemIndex:(int)index
{
    for (UIView *subView in self.subviews) {
        if (subView.tag == 888 + index) {
             [subView removeFromSuperview];
        }
    }
}






@end
